<template>
  <div class="list-table">
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('日期')" prop="exchangeDate">
        <el-date-picker
          v-model="queryParams.timeFormat"
          clearable
          style="width: 200px"
          type="date"
          value-format="yyyy-MM-dd"
          :placeholder="$t('日期')"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">
          {{ $t('搜索') }}
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" tooltip-effect="darkTable" :data="exchangeRateDetailList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('转换前币种')" align="center" prop="toCurrency" :formatter="toCurrencyFormat" />
      <el-table-column :label="$t('转换后币种')" align="center" prop="fromCurrency" :formatter="fromCurrencyFormat" />
      <el-table-column :label="$t('最新汇率')" align="center" prop="exchangeRate" width="180">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.exchangeRate, 'rate') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('日期')" align="center" prop="exchangeDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.exchangeDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('备注信息')" align="center" prop="remarks" />
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['exchange:sys:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改汇率转换对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <exchangeRateDetailForm ref="exchangeRateDetailForm" />
    </el-dialog>
  </div>
</template>

<script>
import {
  listExchangeRateDetail,
  delExchangeRateDetail
} from '@/api/system/exchange/exchangeRateDetail'
import exchangeRateDetailForm from './exchangeRateDetailForm.vue'

export default {
  components: {
    exchangeRateDetailForm
  },
  props: {
    exchangeId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 汇率转换表格数据
      exchangeRateDetailList: [],
      // 弹出层标题
      title: '汇率转换',
      // 是否显示弹出层
      formOpen: false,
      // 币种字典
      currencyOptions: [],
      // 查询参数
      queryParams: {
        exchangeId: this.exchangeId,
        timeFormat: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  watch: {
    exchangeId: {
      handler(val) {
        if (val) {
          this.queryParams.exchangeId = val
          this.getList()
        }
      },
      deep: true,
      immediate: true

    }
  },
  created() {
    this.getList()
    this.getDicts('currency').then(response => {
      this.currencyOptions = response.data
    })
  },
  methods: {
    /** 查询汇率转换列表 */
    getList() {
      this.loading = true
      listExchangeRateDetail(this.queryParams).then(response => {
        this.exchangeRateDetailList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 转换后币种字典翻译
    fromCurrencyFormat(row) {
      return this.selectDictLabel(this.currencyOptions, row.fromCurrency)
    },
    // 转换前币种字典翻译
    toCurrencyFormat(row) {
      return this.selectDictLabel(this.currencyOptions, row.toCurrency)
    },

    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        exchangeId: undefined,
        fromCurrency: undefined,
        toCurrency: undefined,
        exchangeRate: undefined,
        exchangeDate: undefined,
        createDate: undefined,
        updateDate: undefined,
        delFlag: undefined,
        remarks: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.exchangeRateDetailForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.exchangeRateDetailForm.init(row)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return delExchangeRateDetail(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(function() {
      })
    }
  }
}
</script>
